package com.group02.community.service.impl;


import com.alibaba.excel.EasyExcel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.log.SysOperLog;
import com.group02.community.mapper.SysOperLogMapper;
import com.group02.community.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author xl
 * @since 2022-07-01
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Autowired
    private SysOperLogMapper sysOperLogMapper;

    @Override
    public IPage<SysOperLog> operLogPage(Long current, Long size, SysOperLog sysOperLog, String dateRange) {
        if (dateRange!=null && dateRange!=""){
            String[] split = dateRange.split(",");
            sysOperLog.setStartTime(split[0]);
            sysOperLog.setEndTime(split[1]);
        }
        IPage<SysOperLog> operLogPage = sysOperLogMapper.operLogPage(new Page<>(current, size),sysOperLog);
        return operLogPage;
    }

    @Override
    public void insertOperlog(SysOperLog operLog) {
        sysOperLogMapper.insertOperlog(operLog);
    }

    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        //查询所有数据
        List<SysOperLog> sysDictTypes = sysOperLogMapper.selectList(null);
        //设置文件下载
        //设置响应头，告诉浏览器要以文件的形式保存
//        response.setHeader();
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlms");
        //生成Excel并写入
        EasyExcel.write(response.getOutputStream(),SysOperLog.class).sheet(System.currentTimeMillis()+"").doWrite(sysDictTypes);
    }

    @Override
    public void emptyDatabase() {
        sysOperLogMapper.emptyDatabase();
    }

    @Override
    public void loginLogDelete(Long[] id) {
        sysOperLogMapper.loginLogDelete(id);
    }
}
